﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Pcb.Common
{
    /// <summary>
    /// Cookie有关操作
    /// </summary>
    public class _Cookie
    {
        /// <summary>
        /// 保存 Cookie 值
        /// </summary>
        /// <param name="strName"> cookie名 </param>
        /// <param name="strValue"> 要保存的值 </param>
        /// <param name="strDay"> 保存时间(单位：天) </param>
        /// <returns></returns>
        public static bool SetCookie(string strName, string strValue, int strDay)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(strName);
                cookie.Expires = DateTime.Now.AddDays((double)strDay);
                cookie.Value = strValue;
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            catch
            {
            }
            return false;
        }

        /// <summary>
        /// 获取 Cookie 值
        /// </summary>
        /// <param name="strName"> cookie名 </param>
        /// <returns></returns>
        public static string GetCookie(string strName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie != null)
            {
                return cookie.Value.ToString();
            }
            return "";
        }

        /// <summary>
        /// 删除 Cookie 值
        /// </summary>
        /// <param name="strName"> cookie名 </param>
        /// <returns></returns>
        public static bool DelCookie(string strName)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(strName);
                cookie.Expires = DateTime.Now.AddDays(-1.0);
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            catch
            {
            }
            return false;
        }

    }
}
